package com.haikesi.api.menuauthority.service.impl;

import com.haikesi.api.base.Constants;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.ErrorTools;
import com.haikesi.api.base.Type;
import com.haikesi.api.menuauthority.dao.AuthorityResourcesDao;
import com.haikesi.api.menuauthority.dao.MenuAuthorityDao;
import com.haikesi.api.menuauthority.domain.po.AuthorityResourcesPO;
import com.haikesi.api.menuauthority.domain.po.MenuAuthorityPO;
import com.haikesi.api.menuauthority.domain.vo.*;
import com.haikesi.api.menuauthority.service.IMenuAuthorityService;
import com.haikesi.exception.BusinessException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * @ClassName:  MenuAuthorityServiceImpl
 * @Description: 菜单增删改查的实现类
 * @author: dx
 * @date:   2019年4月30日 下午12:33:25   
 * @version V1.0
 * @Copyright: 2019 重庆海克斯科技有限公司. 
 * 注意：本内容仅限于重庆海克斯科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
@Transactional
public class MenuAuthorityServiceImpl implements IMenuAuthorityService {
    @Autowired
    private MenuAuthorityDao menuAuthorityDao;
    @Autowired
    private AuthorityResourcesDao authorityResourcesDao;

    @Override
    public List<MenuAuthorityVO> getList(MenuAuthoritySearchVO searchVO) {
        return menuAuthorityDao.getList(searchVO);
    }

    @Override
    public List<MenuAuthorityTreeVO> getTreeList(MenuAuthorityTreeSearchVO searchVO) {
        List<MenuAuthorityTreeVO> retunList=new ArrayList<>();
        List<MenuAuthorityTreeVO> list=menuAuthorityDao.getTreeList(searchVO);
        HashMap<String,MenuAuthorityTreeVO> map=new HashMap<>();
        for (MenuAuthorityTreeVO vo:list) {
            map.put(vo.getId(),vo);
        }
        for (MenuAuthorityTreeVO vo:list) {
            if(vo.getParentId().equals("0")){
                retunList.add(vo);
            }else {
                MenuAuthorityTreeVO parentVO=map.get(vo.getParentId());
                if(parentVO.getChildrens()==null){
                    parentVO.setChildrens(new ArrayList<>());
                }
                parentVO.getChildrens().add(vo);
            }
        }
        return retunList;
    }

    @Override
    public String create(MenuAuthoritySaveVO vo) {
        MenuAuthorityPO po =new MenuAuthorityPO();
        BeanUtils.copyProperties(vo, po);
        po.preInsert();
        int i=menuAuthorityDao.insertSelective(po);
        if(i== Constants.SQL_RETUN_SUCCESS){
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
        return po.getId();
    }

    @Override
    public void update(MenuAuthorityUpdateVO updateVO) {
        MenuAuthorityPO po =new MenuAuthorityPO();
        BeanUtils.copyProperties(updateVO, po);
        po.preUpdate();
        int i=menuAuthorityDao.updateByPrimaryKeySelective(po);
        if(i== Constants.SQL_RETUN_SUCCESS){
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
    }
    @Override
    public void deleteMenu(String id) {
//        MenuAuthorityPO po =new MenuAuthorityPO();
//        po.setId(id);
//        po.preDelete();
//        menuAuthorityDao.updateByPrimaryKeySelective(po);
        if (StringUtils.isBlank(id)) {
            throw new BusinessException(Type.FORMAT_EXCEPTION);
        }
        MenuAuthorityPO menuAuthorityPO = menuAuthorityDao.selectByPrimaryKey(id);
        if (menuAuthorityPO == null) {
            throw new BusinessException(Type.NOT_FOUND_ERROR, ErrorTools.ErrorAsArrayList(new Error("id", "机构不存在")));
        }
        MenuAuthorityPO po =new MenuAuthorityPO();
        po.setId(id);
        po.preDelete();
        int i = menuAuthorityDao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETUN_SUCCESS) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
        //删除下级的菜单
        menuAuthorityDao.deleteByParentId(id);


    }

    @Override
    public int getNextMenuCount(String id) {
        return menuAuthorityDao.getNextMenuCount(id);
    }

    @Override
    public void addResource(MenuAuthorityResourceVO menuAuthorityResourceVO) {
        if (menuAuthorityResourceVO.getResourceIds().size() < 1) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL);
        }
        AuthorityResourcesPO po =new AuthorityResourcesPO();

        String authorityId=menuAuthorityResourceVO.getMenuAuthorityId();
        List<String> oldResourceIds = menuAuthorityResourceVO.getOldResourceIds();
        authorityResourcesDao.deleteByMenuReId(authorityId,oldResourceIds);
        po.setAuthorityId(menuAuthorityResourceVO.getMenuAuthorityId());
        for (String resourceId : menuAuthorityResourceVO.getResourceIds()) {
            if(!(resourceId.equals(""))){
                po.setResourcesId(resourceId);
                authorityResourcesDao.insertSelective(po);
            }
        }
    }



}
